package com.an.blog.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.an.blog.admin.domain.BlogArticle;
import com.an.blog.admin.domain.BlogCategory;
import com.an.blog.admin.domain.bo.BlogCategoryBo;
import com.an.blog.admin.domain.vo.BlogCategoryVo;
import com.an.blog.mapper.BlogArticleMapper;
import com.an.blog.mapper.BlogCategoryMapper;
import com.an.blog.service.IBlogCategoryService;
import com.an.blog.web.domain.vo.BlogCategoryWebVo;
import com.an.common.core.domain.PageQuery;
import com.an.common.core.page.TableDataInfo;
import com.an.common.exception.blog.BlogException;
import com.an.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 博客分类Service业务层处理
 *
 * @author Ann
 * @date 2023-05-08
 */
@RequiredArgsConstructor
@Service
public class BlogCategoryServiceImpl implements IBlogCategoryService {

    private final BlogCategoryMapper baseMapper;
    private final BlogArticleMapper articleMapper;


    /**
     * 查询博客分类
     */
    @Override
    public BlogCategoryVo queryById(Long categoryId) {
        return baseMapper.selectVoById(categoryId);
    }

    /**
     * 查询博客分类列表
     */
    @Override
    public TableDataInfo<BlogCategoryVo> queryPageList(BlogCategoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BlogCategory> lqw = buildQueryWrapper(bo);
        Page<BlogCategoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询博客分类列表
     */
    @Override
    public List<BlogCategoryVo> queryList(BlogCategoryBo bo) {
        LambdaQueryWrapper<BlogCategory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BlogCategory> buildQueryWrapper(BlogCategoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BlogCategory> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getCategoryName()), BlogCategory::getCategoryName, bo.getCategoryName());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), BlogCategory::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增博客分类
     *
     * @param bo 业务对象
     * @return {@link Boolean }
     * @author: Ann
     * @createDate: 2023/05/11 12:38:00
     * @version: 1.0.0
     */
    @Override
    public Boolean insertByBo(BlogCategoryBo bo) {
        BlogCategory add = BeanUtil.toBean(bo, BlogCategory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCategoryId(add.getCategoryId());
        }
        return flag;
    }

    /**
     * 修改博客分类
     *
     * @param bo 业务对象
     * @return {@link Boolean }
     * @author: Ann
     * @createDate: 2023/05/11 12:38:02
     * @version: 1.0.0
     */
    @Override
    public Boolean updateByBo(BlogCategoryBo bo) {
        BlogCategory update = BeanUtil.toBean(bo, BlogCategory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体
     * @author: Ann
     * @createDate: 2023/05/11 12:46:45
     * @version: 1.0.0
     */
    private void validEntityBeforeSave(BlogCategory entity) {
        if (StringUtils.isNotEmpty(entity.getCategoryName()) && !checkCategoryNameUnique(entity)) {
            throw new BlogException("blog.category.add.not.unique");
        }
    }

    /**
     * 检查类别名称独特
     *
     * @param entity 实体
     * @return boolean
     * @author: Ann
     * @createDate: 2023/05/11 12:42:07
     * @version: 1.0.0
     */
    private boolean checkCategoryNameUnique(BlogCategory entity) {
        boolean exist = baseMapper.exists(new LambdaQueryWrapper<BlogCategory>()
            .eq(BlogCategory::getCategoryName, entity.getCategoryName())
            .ne(ObjectUtil.isNotNull(entity.getCategoryId()), BlogCategory::getCategoryId, entity.getCategoryId()));
        return !exist;
    }

    /**
     * 批量删除博客分类
     *
     * @param ids     id
     * @param isValid 是有效
     * @return {@link Boolean }
     * @author: Ann
     * @createDate: 2023/05/11 11:41:57
     * @version: 1.0.0
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            boolean exists = articleMapper.exists(new LambdaQueryWrapper<BlogArticle>()
                .in(CollUtil.isNotEmpty(ids), BlogArticle::getCategoryId, ids));
            if (exists) {
                throw new BlogException("blog.category.delete.error");
            }
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 更新类别状态
     *
     * @param bo 业务对象
     * @return int
     * @author: Ann
     * @createDate: 2023/05/11 11:45:53
     * @version: 1.0.0
     */
    @Override
    public int updateCategoryStatus(BlogCategoryBo bo) {
        BlogCategory blogCategory = BeanUtil.toBean(bo, BlogCategory.class);
        return baseMapper.updateById(blogCategory);
    }

    /**
     * 查询通过id列表
     *
     * @param categoryIdSet 类别id设置
     * @return {@link List }<{@link BlogCategoryVo }>
     * @author: Ann
     * @createDate: 2023/05/10 21:23:03
     * @version: 1.0.0
     */
    @Override
    public List<BlogCategoryVo> queryListByIds(Collection<Long> categoryIdSet) {
        return baseMapper.selectVoList(new LambdaQueryWrapper<BlogCategory>()
            .in(CollUtil.isNotEmpty(categoryIdSet), BlogCategory::getCategoryId, categoryIdSet));
    }

    @Override
    public List<BlogCategoryWebVo> queryCategoryWebList() {
        List<BlogCategory> blogCategories = baseMapper.selectList(new LambdaQueryWrapper<BlogCategory>()
            .select(BlogCategory::getCategoryId,BlogCategory::getCategoryName));
        return BeanUtil.copyToList(blogCategories,BlogCategoryWebVo.class);
    }
}
